﻿using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace WebAppEF.Models;

public partial class DbtestContext : DbContext
{
    public DbtestContext()
    {
    }

    public DbtestContext(DbContextOptions<DbtestContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Dept> Depts { get; set; }

    public virtual DbSet<Employee> Employees { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseSqlServer("Data Source=SAN-PC\\WINCCPLUSMIG2014;Initial Catalog=DBTEST;Persist Security Info=True;User ID=sa;Password=sa;TrustServerCertificate=true");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Dept>(entity =>
        {
            entity.HasKey(e => e.DeptId).HasName("PK__Dept__014881AE8A85E0AC");

            entity.ToTable("Dept");

            entity.Property(e => e.DeptName)
                .HasMaxLength(50)
                .IsUnicode(false);
        });

        modelBuilder.Entity<Employee>(entity =>
        {
            entity.HasKey(e => e.EmpId).HasName("PK__Employee__AF2DBB994948F969");

            entity.ToTable("Employee");

            entity.Property(e => e.EmpArea)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.EmpName)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.EmpPhone)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.EmpSalary).HasColumnType("decimal(18, 2)");
        });

        #region 新增 使用EF Core框架实体类本身的关系关联来进行关联数据的查询

        modelBuilder.Entity<Employee>()
            .HasOne<Dept>(c => c.TheDept)
            .WithMany(a => a.Emps)
            .HasForeignKey(c => c.DeptId);//外键
        
        #endregion


        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
